/**                              **/
var maxFanNumber = 18; //可以邀请的粉丝人数上限
var fanSelectedId = -1; //输入框自动提醒时 所选中的粉丝的ID
var portraitName = "";
var nickName = "";
var allSelectedFanIdArray = new Array(maxFanNumber); // 储存已添加的粉丝id
var allSelectedFanNicknameArray = new Array(maxFanNumber);// 储存已添加的粉丝昵称
var allSelectedFanPortraitArray = new Array(maxFanNumber); //储存已添加的粉丝头像

var allSelectedFanNumber = 0;//记录储存的粉丝个数

//初始化
for ( var i = 0; i < allSelectedFanNicknameArray.length; i++) {
	allSelectedFanIdArray[i] = -1;
	allSelectedFanNicknameArray[i] = "";
	allSelectedFanPortraitArray[i] = "";
}

/**
 * 添加好友时查看是否有重复
 * 
 * @param lastText
 * @param name
 * @returns {Boolean} 1 可以添加 0 超出最大邀请人数 -1 不能添加
 */
function checkFans(tempId) {

	var result = 1;

	if (allSelectedFanNumber >= maxFanNumber)
		return 0;

	for ( var i = 0; i < allSelectedFanIdArray.length; i++) {
		if (allSelectedFanIdArray[i] == tempId) {
			result = -1;
			break;
		}
	}

	return result;
}

/**
 * 将邀请的粉丝作记录
 * @param tempId
 */
function addFanToArray(tempId, tempNickname, tempPortrait) {

	for ( var i = 0; i < allSelectedFanIdArray.length; i++) {
		if (allSelectedFanIdArray[i] == -1) {
			allSelectedFanIdArray[i] = tempId;
			allSelectedFanNicknameArray[i] = tempNickname;
			allSelectedFanPortraitArray[i] = tempPortrait;

			allSelectedFanNumber++;
			break;
		}
	}
}
/**
 * 删除粉丝，
 * array清除记录
 */
function deleteFanFromArray(tempId) {

	for ( var i = 0; i < allSelectedFanIdArray.length; i++) {
		if (allSelectedFanIdArray[i] == tempId) {
			allSelectedFanIdArray[i] = -1;
			allSelectedFanNicknameArray[i] = "";
			allSelectedFanPortraitArray[i] = "";
			allSelectedFanNumber--;
			break;
		}
	}
}

/**
 * 移除粉丝
 */
// 注：divid传进来的是整个容器
function onFanItemSeleted_delete(fanId) {
	fanSelectedId = fanId;

	var parentDiv = document.getElementById("fanListInvitedDiv");
	var toDeleteDiv = document.getElementById("fanItemSelectedDiv"
			+ fanSelectedId);

	parentDiv.removeChild(toDeleteDiv);
	deleteFanFromArray(fanSelectedId);

	//将粉丝列表中的“已邀请”改为“邀请”
	var tempAddImgUrl = SKYLINE_DOMAIN + "comment/imagepath/add.png";
	var fanInvitingSubDiv = document.getElementById("fanInvitingDiv"
			+ fanSelectedId);
	var ind = fanInvitingSubDiv.childNodes.length;
	for ( var i = ind - 1; i >= 0; i--) {
		fanInvitingSubDiv.removeChild(fanInvitingSubDiv.childNodes[i]);
	}

	fanInvitingSubDiv.innerHTML += ("<img src=" + tempAddImgUrl + " >");
	var onclickUrl = "onFanItemSeleted_add('" + fanSelectedId + "','"
			+ portraitName + "','" + nickName + "');";
	fanInvitingSubDiv.innerHTML += ("<a href='#' onclick=" + onclickUrl + " >邀请</a>");

}

/**
 * 邀请fan 选中某个Item
 */
function onFanItemSeleted_add(fanId, portait, tempNickName) {
	fanSelectedId = fanId;
	portraitName = portait;
	nickName = tempNickName;

	if (fanSelectedId != -1) {

		if (portraitName == "" && portraitName.equals(""))
			portraitName = "default";

		var fanListInvitedDiv = document.getElementById("fanListInvitedDiv");

		// 该粉丝未添加
		var result = checkFans(fanSelectedId);
		if (result == 1) {
			addFanToArray(fanSelectedId, nickName, portraitName);

			var tempImgUrl = SKYLINE_DOMAIN + "comment/imagepath/"
					+ portraitName + ".jpg";
			var tempNickNameUrl = SKYLINE_DOMAIN + "wo/" + fanSelectedId
					+ ".html >";
			var tempRemoveImgUrl = SKYLINE_DOMAIN
					+ "comment/imagepath/remove.png";

			var tempAddImgUrl = SKYLINE_DOMAIN + "comment/imagepath/add.png";

			newDiv = document.createElement("div");
			var divid = "fanItemSelectedDiv" + fanSelectedId;
			newDiv.id = divid;

			var table = document.createElement("table");
			var tr = document.createElement("tr");
			var td1 = document.createElement("td");

			td1.innerHTML += "<img onclick='onFanItemSeleted_delete("
					+ fanSelectedId + ");'" + " title='单击取消邀请 " + nickName
					+ "'" + " height='60px' width='60px' src=" + tempImgUrl
					+ " >";
			tr.appendChild(td1);

			var td2 = document.createElement("td");
			td2.innerHTML += ("<a href=" + tempNickNameUrl + nickName + "</a> ");

			var p = document.createElement("p");///comment/imagepath/remove.png
			p.innerHTML += ("<img src=" + tempRemoveImgUrl + " >");
			p.innerHTML += ("<a href=" + "'javascript:onFanItemSeleted_delete("
					+ fanSelectedId + ")'" + ">取消邀请</a> ");
			td2.appendChild(p);
			tr.appendChild(td2);
			table.appendChild(tr);

			newDiv.appendChild(table);
			fanListInvitedDiv.appendChild(newDiv);

			//将 “邀请” 变更为 “已邀请” 
			var fanInvitingSubDiv = document.getElementById("fanInvitingDiv"
					+ fanSelectedId);
			var ind = fanInvitingSubDiv.childNodes.length;
			for ( var i = ind - 1; i >= 0; i--) {
				fanInvitingSubDiv.removeChild(fanInvitingSubDiv.childNodes[i]);
			}

			fanInvitingSubDiv.innerHTML += ("<img src=" + tempAddImgUrl + " >已邀请");

		} else if (result == 0) {
			alert("对不起,您最多只能邀请 " + maxFanNumber + " 人.");
		} else {
			alert(nickName + " 已邀请!");
		}
	}
}

/************************************表单 提交****** ***************/
function inviteFormSub() {
	var result = true;
	var appointmentTime = document.getElementById("appointmentTime");
	var currentTime = new Date().format("yyyy-MM-dd hh:mm:ss").toString();

	
	//预约时间不能为空
	if (appointmentTime.value == "") {
		alert("请选择预约的时间！");
		result = false;
	} else if (currentTime >= appointmentTime.value) {
		alert("您的时间已过期！请重新检查！");
		result = false;
	}
	//邀请的粉丝不能为空
	else if (allSelectedFanNumber <= 0) {
		alert("至少邀请一个粉丝！");
		result = false;
	}

	//验证过后才提交
	//注入数据
	if (result == true) {
		var tempFanIdList = document.getElementById("fanIdList");
		var tempFanNicknameList = document.getElementById("fanNicknameList");
		var tempFanPortraitList = document.getElementById("fanPortraitList");

		tempFanIdList.value = allSelectedFanIdArray.join(";").toString();
		tempFanNicknameList.value = allSelectedFanNicknameArray.join(";")
				.toString();
		tempFanPortraitList.value = allSelectedFanPortraitArray.join(";")
				.toString();

		$("#inviteForm").submit();
	}

}
